package com.example.demo.school.repositories;

import com.example.demo.school.models.Student;
import com.example.demo.school.models.search.StudentSearch;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class StudentRepository extends BaseRepository<Student> {


    public List<Student> findBySearch(StudentSearch search) {
        Map<String, Object> params = new HashMap();
        String hql = "from Student a where 1=1 ";
        if (search.getName() != null && !search.getName().trim().equals("")) {
            hql += " and a.name like :name";
            params.put("name", "%" + search.getName() + "%");
        }
        if (search.getTypeId() != null) {
            hql += " and a.type.id=:typeId";
            params.put("typeId", search.getTypeId());
        }
        Query query = this.createQuery(hql);
        params.forEach(query::setParameter);
        return query.getResultList();
    }
}

